// 04myCenter/11shoppingOrder/01spOrderDetail/spOrderDetail.js
var co = require('../../../resource/js/common.js')
var M = require("../../../resource/js/md5.js")
var _this, _data
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
     spOrderDetail:'',  //订单详情
     expressCost:'',    //快递费用
     expressInfo:'',    //订单信息
     showExpress:false,  //物流信息弹出
     orderID:'',//订单ID
     money:'',//订单价格
     userInfo:'',//用户信息
     expressProgress:'',//物流进度
     address:'',//收货地址
     //distributionInfo:'',//配送信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     _this = this
     _data = this.data
     //console.log('options',options)
     //console.log('app.Data',app.Data)
     var orderID = options.orderID
     this.spOrderDetail(options.orderID)
     this.setData({
        orderID: orderID,
        userInfo:app.Data.userInfo
     })
     this.getUserAddress(app.Data.userInfo.User_ID)
  },

  //获取订单详情
  spOrderDetail:function(orderID){
     var url = 'jyhs/Ordmg/getOrder'
     var _expressProgress//物流进度
     var _distributionInfo//配送信息
     co.getMealApi(url, {
        Order_ID: orderID
     }, function (res) {
        if(res.code == 0){
           var orderDetail = res.data
           var expressInfo = orderDetail.Order_Shipping_Info.shipping
           if (orderDetail.Order_status == 1 || orderDetail.Order_status == 2){
              _expressProgress = '正在安排发货'
              //_distributionInfo = '正在努力筹备中'
           } else if (orderDetail.Order_status == 3 || orderDetail.Order_status == 6){
              _expressProgress = '订单已取消'
              //_distributionInfo = '你的订单被外星人带走了'
           } else if (orderDetail.Order_status == 4 || orderDetail.Order_status == 7) {
              _expressProgress = '订单已完成'
           } else if (orderDetail.Order_status == 5){
              if (orderDetail.Order_Shipping_Info.type == 'bizsend'){
                 _expressProgress = '商家配送'
                // _distributionInfo = '商家正在赶来的路上'
              } else if (orderDetail.Order_Shipping_Info.type == "express"){
                 _expressProgress = orderDetail.Order_Shipping_Info.shipping.express_name
              }
           }
           //console.log('expressInfo', orderDetail)
           _this.getExpressCost(orderDetail.Order_CartList)//获取快递费用
           if (expressInfo){//判断是否发货
              _this.getExpressInfo(expressInfo.express_name, expressInfo.shipping_id)              
           }   
           _this.setData({
             // distributionInfo: _distributionInfo,
              expressProgress:_expressProgress,
              spOrderDetail: orderDetail,
              money: orderDetail.Order_TotalAmount
           })        
           
        }
        //console.log(res)
     })
  },
  //获取快递信息
  getExpressInfo:function(companyName,expressID){
     var url = 'jyhs/Shippingmg/getExpressInfo'
     var param = {
        express: companyName,
        express_id: expressID
     }
     co.getMealApi(url,param,function(res){
        if(res.code == 0){
           _this.setData({
              expressInfo:res.data
           })
           //console.log('快递状态',res)
        }
     })
  },
  //跳转到店铺
  toShop:function(){
     wx.switchTab({
        url: '/05shoppingMall/shoppingMall',
     })
  },
  //获取快递费用
   getExpressCost: function (orderList){
     var url = 'jyhs/Shippingmg/getExpressCost'
     var param = {
        Order_CartList: JSON.stringify(orderList)
     }
     co.getMealApi(url,param,function(res){
        if(res.code == 0){
           _this.setData({
              expressCost:res.data
           })
        }
        //console.log('res费用',res)     
     })
  },
  //弹出物流信息详情
   showExpressDetail:function(){
      if (_data.expressInfo){
         _this.setData({
            showExpress: true
         })
      }
      
   },
   //关闭弹窗
   maskHide:function(){
      _this.setData({
         showExpress:false
      })
   },
   //取消订单
   cancalOrder: function (e) {
      var order_ID = e.currentTarget.dataset.orderid
      var status = 3
      _this.changeOrderStatus(order_ID, status)
      
   },
   //付款
   payOrder: function (e) {
      console.log('e', e)
      var order_ID = e.currentTarget.dataset.orderid
      var money = _data.spOrderDetail.Order_TotalAmount
      this.setData({
         money: money,
         orderID: order_ID,
         payMode: true
      })      
   },
   //收货
   confirmOrder: function (e) {
      var order_ID = e.currentTarget.dataset.orderid
      var status = 7
      _this.changeOrderStatus(order_ID, status)
   },
   //评价


   //更改订单状态
   changeOrderStatus: function (orderID, status) {
      var url = 'jyhs/Ordmg/updateStatus'
      var param = {
         Order_ID: orderID,
         Order_Status: status
      }
      co.getMealApi(url, param, function (res) {
         if (res.code == 0) {
            _this.spOrderDetail(_data.orderID)
            
         }
      })

   },
   // 选择的支付方式
   payMode: function (e) {
      var mode = e.currentTarget.dataset.mode
      var userInfo = app.Data.userInfo
      if (mode == 1) {
         //判断是否设置支付密码
         if (userInfo.PayPassword_Is_Set == 0) {
            co.confirm('未设置支付密码', '取消', '前往设置', function (res) {
               if (res) {
                  wx.navigateTo({
                     url: "/04myCenter/02set/02bindPhone/bindPhone",
                  })
               }
            })
            return
         }
         //如果余额为零自动跳转为微信支付
         if (userInfo.User_Balance == 0) {
            co.toast('您的账户余额为零，请充值')
            return
         }
         if (parseInt(userInfo.User_Balance) < parseInt(this.data.total)) {
            co.toast('您的余额不足，请充值')
            return
         }
         this.setData({
            payMode: false,
            pasInput: true
         })
      } else if (mode == 2) {
         this.pay(_data.money)
         this.setData({
            payMode: false
         })
      }
   },
   //关闭弹窗
   payClose: function () {
      _this.setData({
         pasInput: false,
         payMode: false
      })
   },
   //添加用户支付记录
   addUserRecord: function (callback) {
      var userInfo = app.Data.userInfo
      var url = 'money/Recorduser/userAdd'
      var uRecord = {
         User_ID: userInfo.User_ID,
         Abstract: '91商城消费',
         Payer: 'Biz_' + 91,
         Money: -_data.money,
         Pay_From: 'wx_pay',
         Pay_To: '91_pay',
         isMall: 1
      }
      co.getJyhsApi(url, uRecord, function (res) {
         if (typeof (callback) == 'function') {
            callback()
         }
         if (res.code == 0) {

         } else {
            co.modal(res.msg)
         }
      })
   },
   //添加商家支付记录
   addBizRecord: function () {
      var userInfo = app.Data.userInfo
      var url = 'money/Recordbiz/bizAdd'
      var bizRecord = {
         Biz_ID: 91,
         Abstract: userInfo.User_NickName + '91商城购物',
         Payer: 'User_' + userInfo.User_ID,
         Money: _data.money,
         Pay_From: 'wx_pay',
         Pay_To: '91_pay',
      }
      co.getJyhsApi(url, bizRecord, function (rs) {
         if (rs.code == 0) {
            //co.modal(res.msg)
         } else {
            //co.modal(res.msg)
         }
      })
   },
   //忘记密码
   retrievePwd: function () {
      wx.navigateTo({
         url: '/04myCenter/02set/02changePayPwd/01retrievePassword/retrievePassword',
      })
   },
   // 微信支付
   pay: function (money) {
      //金额为订单总价
      var appid = app.Data.app.appid
      var param = { //e.detail.value
         openid: app.Data.userInfo.Wx_Openid[appid],
         appid: appid,
         money: money,
         abstract: "旧约商城购物"
      }
      //console.log('微信param', param)
      co.getShareApi('api/Wxpay/createParam', param, function (res) {
         console.log('微信支付',res)
         res.data.success = function (result) {
            //for (var i in orderID) {
            //添加用户支付记录
            _this.addUserRecord(function (res) {
               //添加商家支付记录。确保数据不易出错，按顺序先加付款方数据，再加收款方数据
               _this.addBizRecord()
            })
            _this.changeOrderStatus(_data.orderID, 2)//修改订单状态
            co.toastAllText('支付成功')
            // }
         }
         res.data.fail = function (result) {
            co.toastAllText('支付失败')
            console.log('【调起支付失败res】', result)
         }
         //调起微信支付
         wx.requestPayment(res.data)
      })
   },
   // 余额支付
   yuePay: function (money, pa) {
      var param = {
         Pay_From_ID: 'User_' + app.Data.userInfo.User_ID,
         Pay_Password: pa,
         Money: money,
         Pay_To_ID: 'Biz_' + 91,
         Abstract: '91商城购物',
         isMall: 1
      }
      co.getJyhsApi('money/Recorduser/balancePay', param, function (res) {
         if (res.code == 0) {
            co.updateUserBalance(res.data.New_Balance) //修改用户余额
            app.getWXUserInfo()//更新用户数据
            _this.changeOrderStatus(_data.orderID, 2)//修改订单状态
            co.toastAllText('支付成功')
            _this.setData({
               pasInput: false
            })

         } else {
            co.toastAllText('支付失败')
         }
      })
   },
   // 余额支付输入密码
   pasForm: function (e) {
      console.log("【获取密码】===", e)
      var pa = M.md5(e.detail.value.pa)
      this.yuePay(_data.money, pa)
   },
   //用户的快递地址
   getUserAddress: function (uid) {
      var param = {
         User_ID: uid
      },
         address
      co.getShareApi('address/Addrmg/getDefaultAddress', param, function (res) {
         if (res.code == 0) {
            address = res.data
            _this.setData({
               address: address
            })
         }
      })
   },











  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})